package com.hunliji.monitor.web.strategy.alert;

import com.google.common.collect.Maps;
import com.hunliji.monitor.share.enums.AlertActionTypeEnum;
import com.hunliji.monitor.web.strategy.alert.model.AlertActionResultBO;
import com.hunliji.monitor.web.strategy.alert.model.AlertContext;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

/**
 * 告警行动处理
 */
@Slf4j
public class AlertActionClient {

    /**
     * 获取商家主页信息
     *
     * @param context 上下文信息
     * @return
     */
    public static AlertActionResultBO action(AlertContext context, AlertActionTypeEnum alertActionTypeEnum) {
        AlertAction alertActionStrategy =
            AlertActionContext.getMerchantIndexStrategy(alertActionTypeEnum);
        return alertActionStrategy.doAction(context);
    }

    public static Map<String, AlertActionResultBO> actions(AlertContext context, String actionTypes) {
        if(StringUtils.isBlank(actionTypes)){
            return null;
        }
        String[] split = actionTypes.split("\\|");
        Map<String, AlertActionResultBO> result = Maps.newHashMap();
        Arrays.stream(split).forEach(actionType -> {
            AlertActionTypeEnum actionTypeEnum = AlertActionTypeEnum.getEnumByCode(actionType);
            if(Objects.isNull(actionTypeEnum)){
                log.warn("执行器 actionType：{} 不存在", actionType);
            } else {
                result.put(actionType, action(context, actionTypeEnum));
            }
        });
        return result;
    }
}
